package com.wb.fw;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
import cn.cmgame.billing.api.GameInterface;

/**
 * startTimeThread开始计费线程。
 * 
 * @author xieliang
 * 
 */
public class TimePay {
	// private static boolean s_hasStarted = false;
	private static Activity sInstance;
	private static TimeCallback mTimeCallback;

	/**
	 * @param handler
	 * @param activity
	 * @param callback
	 * @param delayTime
	 *            ,秒。
	 */
	public static void startTimeThread(Handler handler, Activity activity,
			long delayTime, TimeCallback callback) {
		Log.d("xltest",
				"GameInterface.getActivateFlag="
						+ GameInterface.getActivateFlag("000"));
		sInstance = activity;
		if (!GameInterface.getActivateFlag("000")) {
			if (!hasPlayed()// 自动退出只调用一次。
			// !s_hasStarted
			) {
				// s_hasStarted = true;
				mTimeCallback = callback;
				handler.postDelayed(mrunnable, delayTime * 1000);
			}
		}
	}

	private static final String KEY = "played";

	public static boolean hasPlayed() {
		SharedPreferences share = sInstance.getSharedPreferences(KEY, 0);
		boolean played = share.getBoolean(KEY, false);
		Log.d("xltest", "time played=" + played);
		return played;
	}

	public static void savePlayer() {
		SharedPreferences share = sInstance.getSharedPreferences(KEY, 0);
		SharedPreferences.Editor editor = share.edit();
		editor.putBoolean(KEY, true);
		editor.commit();
	}

	private static Runnable mrunnable = new Runnable() {
		@Override
		public void run() {
			savePlayer();
			if (mTimeCallback != null) {
				mTimeCallback.startPay();
			}
			Log.d("xltest", "time is used....");
			// MediaGlobal._shared().pauseMusic();
			//
			// if(GameLayer.sharedGameLayer() != null){
			// GameLayer.sharedGameLayer().onPause(null);
			// }
			// CCDirector.sharedDirector().pause();
			GameInterface.doBilling(sInstance, true, false, "000",
					new GameInterface.BillingCallback() {

						@Override
						public void onUserOperCancel(String arg0) {
							if (mTimeCallback != null) {
								mTimeCallback.payFailed();
							}
						}

						@Override
						public void onBillingSuccess(String arg0) {
							if (mTimeCallback != null) {
								mTimeCallback.paySuccess();
							}
						}

						@Override
						public void onBillingFail(String arg0) {
							if (mTimeCallback != null) {
								mTimeCallback.payFailed();
							}
						}
					});
		}
	};

	/**
	 * 调用这个。
	 * 
	 * @param callback
	 */
	public static void doBilling(final TimeCallback callback) {
		GameInterface.doBilling(sInstance, true, false, "000",
				new GameInterface.BillingCallback() {

					@Override
					public void onUserOperCancel(String arg0) {
						if (callback != null) {
							callback.payFailed();
						}
					}

					@Override
					public void onBillingSuccess(String arg0) {
						if (callback != null) {
							callback.paySuccess();
						}
					}

					@Override
					public void onBillingFail(String arg0) {
						if (callback != null) {
							callback.payFailed();
						}
					}
				});
	}

	public interface TimeCallback {
		/**
		 * 弹出计费框前调用.
		 */
		public void startPay();

		/**
		 * 计费成功调用.
		 */
		public void paySuccess();

		/**
		 * 计费失败调用.
		 */
		public void payFailed();
	}

}
